<!--
 * @Descripttion: (车间管理/fac_workshop)
 * @Author: (admin)
 * @Date: (2025-03-30)
-->
<template>
	<div>
		<el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
			<el-form-item label="车间编码" prop="workshopCode">
				<el-input v-model="queryParams.workshopCode" placeholder="请输入车间编码" />
			</el-form-item>
			<el-form-item label="车间名称" prop="workshopName">
				<el-input v-model="queryParams.workshopName" placeholder="请输入车间名称" />
			</el-form-item>
			<el-form-item label="所属工厂" prop="factoryId">
				<el-select clearable v-model="queryParams.factoryId" placeholder="请选择工位类型">
					<el-option v-for="item in options.factoryList" :key="item.factoryId" :label="item.factoryName"
						:value="item.factoryId">
						<span class="fl">{{ item.factoryName }}</span>
						<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.factoryCode }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
				<el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="15" class="mb10">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['fac:workshop:add']" plain icon="plus" @click="handleAdd">
					{{ $t('btn.add') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['fac:workshop:edit']" plain icon="edit"
					@click="handleUpdate">
					{{ $t('btn.edit') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['fac:workshop:delete']" plain icon="delete"
					@click="handleDelete">
					{{ $t('btn.delete') }}
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
			highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="workshopId" label="车间Id" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('workshopId')" />
			<el-table-column prop="factoryId" label="所属工厂Id" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('factoryId')" />
			<el-table-column prop="factoryInfo" label="所属工厂" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('factoryInfo')" />
			<el-table-column prop="workshopCode" label="车间编码" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('workshopCode')" />
			<el-table-column prop="workshopName" label="车间名称" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('workshopName')" />
			<el-table-column label="修改记录" width="90" align="center" show-overflow-tooltip
				v-if="columns.showColumn('updateRecord')">
				<template #default="scope">
					<modifyRecord :data="scope.row" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" width="150">
				<template #default="scope">
					<el-button-group>
						<el-button text icon="edit" @click="handleUpdate(scope.row)" v-hasPermi="['fac:workshop:edit']">
							{{ $t('btn.edit') }}
						</el-button>
						<el-button text icon="delete" @click="handleDelete(scope.row)" v-hasPermi="['fac:workshop:delete']">
							{{ $t('btn.delete') }}
						</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
			@pagination="getList" />


		<el-dialog draggable :close-on-click-modal="false" :title="title" :lock-scroll="false" v-model="open" width="40%">
			<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20">
					<!-- <el-col :lg="24">
            <el-form-item label="车间Id" prop="workshopId">
              <el-input v-model="form.workshopId" placeholder="请输入车间Id" :disabled="opertype != 1" />
            </el-form-item>
          </el-col> -->
					<el-col :lg="24">
						<el-form-item label="车间编码" prop="workshopCode">
							<el-input v-model="form.workshopCode" placeholder="请输入车间编码" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="车间名称" prop="workshopName">
							<el-input v-model="form.workshopName" placeholder="请输入车间名称" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="所属工厂" prop="factoryId">
							<el-select clearable v-model="form.factoryId" placeholder="请选择工厂信息" style="width: 100%;">
								<el-option v-for="item in options.factoryList" :key="item.factoryId" :label="item.factoryName"
									:value="item.factoryId">
									<span class="fl">{{ item.factoryName }}</span>
									<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.factoryCode }}</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="opertype != 3">
				<el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
				<el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="facworkshop">
import modifyRecord from '@/components/MyTable/modifyRecord.vue';
import { listFacFactory } from '@/api/facManage/facFactory.js'
import { pageFacWorkshop, addFacWorkshop, delFacWorkshop, updateFacWorkshop, getFacWorkshop } from '@/api/facManage/facWorkshop.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	sort: '',
	sortType: 'asc',
	workshopCode: undefined,
	workshopName: undefined,
	factoryId: undefined,
})
const columns = ref([
	{ visible: false, prop: 'workshopId', label: '车间Id', },
	{ visible: false, prop: 'factoryId', label: '所属工厂id', },
	{ visible: true, prop: 'factoryInfo', label: '所属工厂', },
	{ visible: true, prop: 'workshopCode', label: '车间编码', },
	{ visible: true, prop: 'workshopName', label: '车间名称', },
	{ visible: true, prop: 'updateRecord', label: '修改记录', },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()

function getList() {
	loading.value = true
	pageFacWorkshop(queryParams).then(res => {
		const { code, data } = res
		if (code == 200) {
			dataList.value = data.result
			total.value = data.totalNum
			loading.value = false
		}
	})
}

// 查询
function handleQuery() {
	queryParams.pageNum = 1
	getList()
}

// 重置查询操作
function resetQuery() {
	proxy.resetForm("queryRef")
	handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map((item) => item.workshopId);
	single.value = selection.length != 1
	multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
	var sort = undefined
	var sortType = undefined

	if (column.prop != null && column.order != null) {
		sort = column.prop
		sortType = column.order

	}
	queryParams.sort = sort
	queryParams.sortType = sortType
	handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
	single: true,
	multiple: true,
	form: {},
	rules: {
		workshopId: [{ required: true, message: "车间Id不能为空", trigger: "blur" }],
		workshopCode: [{ required: true, message: "车间编码不能为空", trigger: "blur" }],
		workshopName: [{ required: true, message: "车间名称不能为空", trigger: "blur" }],
		factoryId: [{ required: true, message: "所属工厂不能为空", trigger: "change" }],
	},
	options: {
		// 工厂信息
		factoryList: []
	}
})
const { form, rules, options, single, multiple } = toRefs(state)

//获取工厂信息
function getFactoryList() {
	listFacFactory({}).then(res => {
		const { code, data } = res
		if (code == 200) {
			state.options.factoryList = data
		}
	})
}

// 关闭dialog
function cancel() {
	open.value = false
	reset()
}

// 重置表单
function reset() {
	form.value = {
		workshopId: null,
		workshopCode: null,
		workshopName: null,
		factoryId: null,
		remark: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
	};
	proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
	reset();
	open.value = true
	title.value = '添加车间管理'
	opertype.value = 1
	form.value.workshopId = proxy.parseGuid();
}
// 修改按钮操作
function handleUpdate(row) {
	reset()
	const id = row.workshopId || ids.value
	getFacWorkshop(id).then((res) => {
		const { code, data } = res
		if (code == 200) {
			open.value = true
			title.value = '修改车间管理'
			opertype.value = 2

			form.value = {
				...data,
			}
		}
	})
}

// 添加&修改 表单提交
function submitForm() {
	proxy.$refs["formRef"].validate((valid) => {
		if (valid) {

			if (form.value.workshopId != undefined && opertype.value === 2) {
				updateFacWorkshop(form.value).then((res) => {
					proxy.$modal.msgSuccess("修改成功")
					open.value = false
					getList()
				})
			} else {
				addFacWorkshop(form.value).then((res) => {
					proxy.$modal.msgSuccess("新增成功")
					open.value = false
					getList()
				})
			}
		}
	})
}

// 删除按钮操作
function handleDelete(row) {
	const Ids = row.workshopId ? [row.workshopId] : undefined || ids.value
	proxy
		.$confirm('是否确认删除"' + Ids.length + '"条数据项？', "警告", {
			confirmButtonText: proxy.$t('common.ok'),
			cancelButtonText: proxy.$t('common.cancel'),
			type: "warning",
		})
		.then(function () {
			return delFacWorkshop(Ids)
		})
		.then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		})
}


handleQuery()
getFactoryList()
</script>